package cn.daniellee.plugin.pl.command;

import cn.daniellee.plugin.pl.PortalLimiter;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class LimiterCommand implements CommandExecutor {
	
	@Override
	public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
		if (commandSender instanceof Player && !commandSender.isOp()) {
			commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("only-op", "Only OP can use this command.")).replace("&", "§"));
			return true;
		}
		if (strings.length == 0 || strings[0].equalsIgnoreCase("help")) {
			sendHelp(commandSender);
			return true;
		}
		if ("reload".equalsIgnoreCase(strings[0])) {
			PortalLimiter.getInstance().reloadConfig();
			PortalLimiter.getInstance().loadConfig();
			commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("reload-success", "Configuration reload completed.")).replace("&", "§"));
		} else if ("size".equalsIgnoreCase(strings[0]) && strings.length > 1) {
			int size = Integer.valueOf(strings[1]);
			PortalLimiter.getInstance().setMaxSize(size);
			PortalLimiter.getInstance().getConfig().set("max-size", size);
			PortalLimiter.getInstance().saveConfig();
			commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("size-set", "Set the maximum number of open blocks successfully.")).replace("&", "§"));
		}  else if ("add".equalsIgnoreCase(strings[0]) && strings.length > 1) {
			String upperType = strings[1].toUpperCase();
			List<String> allList = Arrays.asList(PortalLimiter.getInstance().getAllTypeArray());
			if (!allList.contains(upperType)) {
				commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("invalid-type", "Invalid entity type.")).replace("&", "§"));
				return true;
			}
			List<String> currentList = PortalLimiter.getInstance().getConfig().getStringList("allow-type");
			if (currentList.contains(upperType)) {
				commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("already-set", "This category has been set up.")).replace("&", "§"));
				return true;
			}
			currentList.add(upperType);
			PortalLimiter.getInstance().getConfig().set("allow-type", currentList);
			PortalLimiter.getInstance().saveConfig();
			commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("add-success", "Add entity type successfully.")).replace("&", "§"));
			commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("current-list", "The types of entities currently allowed are") + "： " + StringUtils.join(currentList.toArray(new String[0]), ", ")).replace("&", "§"));
		} else if ("remove".equalsIgnoreCase(strings[0]) && strings.length > 1) {
			String upperType = strings[1].toUpperCase();
			List<String> allList = Arrays.asList(PortalLimiter.getInstance().getAllTypeArray());
			if (!allList.contains(upperType)) {
				commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("invalid-type", "Invalid entity type.")).replace("&", "§"));
				return true;
			}
			List<String> currentList = PortalLimiter.getInstance().getConfig().getStringList("allow-type");
			if (!currentList.contains(upperType)) {
				commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("not-set", "There is no such category in the current list.")).replace("&", "§"));
				return true;
			}
			List<String> resultList = currentList.stream().filter(item -> !item.equals(upperType)).collect(Collectors.toList());
			PortalLimiter.getInstance().getConfig().set("allow-type", resultList);
			PortalLimiter.getInstance().saveConfig();
			commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("remove-success", "Successful removal of entity type.")).replace("&", "§"));
			if (resultList.isEmpty()) commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("empty-list", "Current list is empty.")).replace("&", "§"));
			else commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("current-list", "The types of entities currently allowed are") + "： " + StringUtils.join(resultList.toArray(new String[0]), ", ")).replace("&", "§"));
		} else if ("list".equalsIgnoreCase(strings[0])) {
			List<String> list = PortalLimiter.getInstance().getConfig().getStringList("allow-type");
			if (list.isEmpty()) commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("empty-list", "Current list is empty.")).replace("&", "§"));
			else commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("current-list", "The types of entities currently allowed are") + "： " + StringUtils.join(list.toArray(new String[0]), ", ")).replace("&", "§"));
		} else if ("enum".equalsIgnoreCase(strings[0])) {
			commandSender.sendMessage((PortalLimiter.getInstance().getPrefix() + PortalLimiter.getInstance().getConfig().getString("enumerate-all", "Can be set to allow the type of entity") + "： " + StringUtils.join(PortalLimiter.getInstance().getAllTypeArray(), ", ")).replace("&", "§"));
		} else sendHelp(commandSender);
		return true;
	}

	private void sendHelp(CommandSender commandSender) {
		commandSender.sendMessage(("&m&a---&m&6--------&3 : " + PortalLimiter.getInstance().getPrefix() + "&3 : &m&a--------&m&6---").replace("&", "§"));

		TextComponent sizeHelp = new TextComponent("/pl size <number>  " + PortalLimiter.getInstance().getConfig().getString("size-help", "Set the maximum number of transfer gates."));
		sizeHelp.setColor(ChatColor.GRAY);
		sizeHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pl size "));
		sizeHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) PortalLimiter.getInstance().getConfig().getString("size-help", "Set the maximum number of transfer gates.")).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(sizeHelp);

		TextComponent addHelp = new TextComponent("/pl add <type>  " + PortalLimiter.getInstance().getConfig().getString("add-help", "Add entity type to allow list."));
		addHelp.setColor(ChatColor.GRAY);
		addHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pl add "));
		addHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) PortalLimiter.getInstance().getConfig().getString("add-help", "Add entity type to allow list.")).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(addHelp);

		TextComponent removeHelp = new TextComponent("/pl remove <type>  " + PortalLimiter.getInstance().getConfig().getString("remove-help", "Remove entity types from the allowed list."));
		removeHelp.setColor(ChatColor.GRAY);
		removeHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pl remove "));
		removeHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) PortalLimiter.getInstance().getConfig().getString("remove-help", "Remove entity types from the allowed list.")).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(removeHelp);

		TextComponent listHelp = new TextComponent("/pl list  " + PortalLimiter.getInstance().getConfig().getString("list-help", "List the currently set entity types."));
		listHelp.setColor(ChatColor.GRAY);
		listHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pl list"));
		listHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) PortalLimiter.getInstance().getConfig().getString("list-help", "List the currently set entity types.")).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(listHelp);

		TextComponent enumHelp = new TextComponent("/pl enum  " + PortalLimiter.getInstance().getConfig().getString("enum-help", "List all configurable entity types."));
		enumHelp.setColor(ChatColor.GRAY);
		enumHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pl enum"));
		enumHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) PortalLimiter.getInstance().getConfig().getString("enum-help", "List all configurable entity types.")).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(enumHelp);

		TextComponent reloadHelp = new TextComponent("/pl reload  " + PortalLimiter.getInstance().getConfig().getString("reload-help", "Reload configuration file."));
		reloadHelp.setColor(ChatColor.GRAY);
		reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pl reload"));
		reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) PortalLimiter.getInstance().getConfig().getString("reload-help", "Reload configuration file.")).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(reloadHelp);
	}
}
